# -*- coding: utf-8 -*-

from uiBasicWidget import *
from uiMainWindow import MainWindow,CtaEngineManager
from dynamic_ui_connection import connection
from program_top.utilities.environment_and_platform import environment_initialisation
from program_top.utilities.csv_and_json_serialisation import clean_load_json
from program_top.utilities.my_timer import my_qt_timer,datetime,timedelta

class customised_window(MainWindow):
	'''继承过的主窗口，动态加载不同的接口'''
	def openCta(self):
		"""打开CTA组件"""
		
		if self._business_config['auto_load_strategys']:#如果要自动启动策略
			try:
				self.widgetDict['ctaM'].showMaximized()
			except KeyError:
				self.widgetDict['ctaM'] = CtaEngineManager(self.mainEngine.ctaEngine, self.eventEngine)
				
				self.widgetDict['ctaM'].showMaximized()
				if self._business_config['auto_load_strategys']:#如果要自动加载策略
					self.widgetDict['ctaM'].load()#主动加载
					self.widgetDict['ctaM'].initAll()#主动初始化
					self.widgetDict['ctaM'].startAll()#主动启动
					pass
		else:#如果不自动启动
			super(customised_window, self).openCta()#调用父类的cta接口，不自动启动策略
			pass
	
	def initMenu(self):
		"""初始化菜单"""
		# 创建操作
		
		# 创建菜单
		environment_initialisation(self)
		custom_config_filename=self._environment_pack['instance_path']['config_file_dir']+'config.json'
		self._business_config=clean_load_json(custom_config_filename)
		
		menubar=self.menuBar()
		sysMenu=menubar.addMenu(u'系统')
		
		# 设计为只显示存在的接口
		
		self.connections={}
		
		for each_gateway_name in self.mainEngine.gatewayDict.keys():
			button_name='连接%s'%(each_gateway_name)
			current_connect_action=QtGui.QAction(button_name.decode('utf-8'), self)
			current_connection=connection(each_gateway_name, self)
			self.connections[each_gateway_name]=current_connection
			current_connect_action.triggered.connect(current_connection.connect)
			sysMenu.addAction(current_connect_action)
			pass
		
		connectDbAction=QtGui.QAction(u'连接数据库', self)
		connectDbAction.triggered.connect(self.mainEngine.dbConnect)
		
		testAction=QtGui.QAction(u'测试', self)
		testAction.triggered.connect(self.test)
		
		exitAction=QtGui.QAction(u'退出', self)
		exitAction.triggered.connect(self.close)
		
		aboutAction=QtGui.QAction(u'关于', self)
		aboutAction.triggered.connect(self.openAbout)
		
		contractAction=QtGui.QAction(u'查询合约', self)
		contractAction.triggered.connect(self.openContract)
		
		drAction=QtGui.QAction(u'行情数据记录', self)
		drAction.triggered.connect(self.openDr)
		
		ctaAction=QtGui.QAction(u'CTA策略', self)
		ctaAction.triggered.connect(self.openCta)
		
		rmAction=QtGui.QAction(u'风险管理', self)
		rmAction.triggered.connect(self.openRm)
		
		sysMenu.addSeparator()
		sysMenu.addAction(connectDbAction)
		sysMenu.addSeparator()
		sysMenu.addAction(exitAction)
		
		functionMenu=menubar.addMenu(u'功能')
		functionMenu.addAction(contractAction)
		functionMenu.addAction(drAction)
		functionMenu.addAction(rmAction)
		
		# 算法相关
		algoMenu=menubar.addMenu(u'算法')
		algoMenu.addAction(ctaAction)
		
		# 帮助
		helpMenu=menubar.addMenu(u'帮助')
		helpMenu.addAction(aboutAction)
		helpMenu.addAction(testAction)
		
		try:
			self.__auto_ctp_ref=self.mainEngine.gatewayDict['auto_ctp']#取得自动启动ctp接口的地址
			current_moment=datetime.now()
			self.__timer=my_qt_timer(current_moment+timedelta(seconds=30), self.openCta)#30秒后启动自己的cta界面
			self.__auto_ctp_ref.ui_register(self)#向auto_ctp注册自己的地址
		except Exception, current_error:
			print current_error.__repr__()
			pass
		pass
	pass